//
//  MyCenterViewController.m
//  DoctorFAQs
//
//  Created by zjq on 17/2/9.
//  Copyright © 2017年 zjq. All rights reserved.
//

#import "MyCenterViewController.h"
#import "LZPesonHeardView.h"
#import "LZPersonFooterView.h"
#import "LZMyTicketViewController.h"
#import "LZComplainViewController.h"
#import "LZAboutViewController.h"
#import "LZShareViewController.h"
#import "LZLoginViewController.h"
#import "LZPersonInfoViewController.h"
#import "JNavigationController.h"
#import "LZUserInfo.h"
#import "JTAlertView.h"
#import "LZLoginViewController.h"
#import "JNavigationController.h"
#import "HomeViewController.h"
@interface MyCenterViewController ()<UITableViewDelegate,UITableViewDataSource,UINavigationControllerDelegate>{
    LZUserInfo *userInfo;
    LZPersonFooterView *footerView;
}
/** tableView */
@property (nonatomic, strong) UITableView *mainTableView;
/** 图片数组 */
@property (nonatomic, strong) NSArray *imageArray;
/** 标题数组 */
@property (nonatomic, strong) NSArray *titleArray;
/** 头部视图 */
@property (nonatomic, strong) LZPesonHeardView *headerView;
/** nav */
@property (nonatomic, strong) JNavigationController *tempNav;
@end


// cell重用标识符
static NSString *cellID = @"lz_personCenterCell";

@implementation MyCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 创建tableView
    userInfo = [LZUserInfo getUserInfo];
    [self createTableView];
    [self initializeData];
    self.navigationController.delegate = self;
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear: animated];
//    [selectedViewController viewWillAppear:animated]
    
    [self refreshUI];
//    [_headerView.iconImageView sd_setImageWithURL:[NSURL URLWithString:userInfo.userIcon]];
}

- (void)initializeData {
    _titleArray = @[@"我的优惠券",@"投诉建议",@"关于我们",@"分享给好友"];
    _imageArray = @[@"center_icon_coupon",@"center_icon_proposal",@"center_icon_about_us",@"center_icon_link"];
    // 背景色
    self.view.backgroundColor = KHColorFromRGB(0xf8f8f8);
}

// 创建tableView
- (void)createTableView {
    
    if (!_mainTableView) {
        _mainTableView = [[UITableView alloc] init];
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        _mainTableView.rowHeight = 70*ysIZE;
        _mainTableView.showsVerticalScrollIndicator = NO;
        _mainTableView.backgroundColor = KHColorFromRGB(0xf8f8f8);
        _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        // 头部视图
        
        _headerView = [[LZPesonHeardView alloc] initWithFrame:CGRectMake(0, 64*ysIZE, SCREEN_Width, 130*ysIZE)];
        [_headerView.clickBtn addTarget:self action:@selector(clickBtnClick) forControlEvents:UIControlEventTouchUpInside];
        
        // 赋值
        _headerView.nameLabel.text = userInfo.userName;
//        _headerView.iconImageView.image= [UIImage imageNamed:@"icon_center_tx"];
//         判断如果没有登录或者没有头像用默认头像
        if (userInfo.userIcon && ![userInfo.userIcon isEqualToString:@""]) {
            [_headerView.iconImageView sd_setImageWithURL:[NSURL URLWithString:userInfo.userIcon] placeholderImage:[UIImage imageNamed:@"icon_center_tx"]];
        }else {
            _headerView.iconImageView.image = [UIImage imageNamed:@"icon_center_tx"];
        }
        
        
        _mainTableView.tableHeaderView = _headerView;
        [self.view addSubview:_mainTableView];
        [_mainTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.equalTo(self.view);
            make.center.equalTo(self.view);
        }];
        
        // 尾部视图
        footerView = [[LZPersonFooterView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_Width, 75*ysIZE)];
        _mainTableView.tableFooterView = footerView;
        [footerView.exitBtn addTarget:self action:@selector(exitButtonClick) forControlEvents:UIControlEventTouchUpInside];
        if (!userInfo.uid || [userInfo.uid isEqualToString:@""]) {
            _mainTableView.tableFooterView.hidden = YES;
        }else {
            _mainTableView.tableFooterView.hidden = NO;
        }
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _titleArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    // 自定义tableView的分割线
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = KHColorFromRGB(0xe0e0e0);
    [cell.contentView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(355*xsIZE);
        make.height.offset(0.5*ysIZE);
        make.left.equalTo(cell.mas_left).offset(10*xsIZE);
        make.bottom.equalTo(cell);
        
    }];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = _titleArray[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:_imageArray[indexPath.row]];
    return cell;
}

// 点击头像那一行
- (void)clickBtnClick {
    if ([self goLogin]) {
        LZPersonInfoViewController *personInfoVC = [[LZPersonInfoViewController alloc] init];
        [self.navigationController pushViewController:personInfoVC animated:YES];
    }
    
}


// 点击退出登录按钮
- (void)exitButtonClick {
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"真的要退出吗？" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *ExitAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"LogOut" object:nil];
        self.navigationController.tabBarController.selectedIndex = 0;
        [LZUserInfo deleteUserInfo];
        
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    [alertVC addAction:cancelAction];
    [alertVC addAction:ExitAction];
    [self presentViewController:alertVC animated:YES completion:nil];
    
}

// cell点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.row) {
        case 0:{
            if ([self goLogin]) {
                LZMyTicketViewController *myTicketVC = [[LZMyTicketViewController alloc] init];
                [self.navigationController pushViewController:myTicketVC animated:YES];
            }
            
        }
        break;
        case 1:{
            if ([self goLogin]) {
                LZComplainViewController *complainVC = [[LZComplainViewController alloc] init];
                [self.navigationController pushViewController:complainVC animated:YES];
            }
            
        }
            break;
        case 2:{
            LZAboutViewController *aboutVC = [[LZAboutViewController alloc] init];
            [self.navigationController pushViewController:aboutVC animated:YES];
        }
            break;
        case 3:{
            if ([self goLogin]) {
                LZShareViewController *shareVC = [[LZShareViewController alloc] init];
                [self.navigationController pushViewController:shareVC animated:YES];
            }
            
        }
            break;
            
        default:
            break;
    }
}


// 刷新个人中心界面(登录成功后刷新)
- (void)refreshUI {
    //登录成功的时候给头像和名字赋值并且判断头像，如果为空或者空字符串就使用默认头像
    _headerView.nameLabel.text = userInfo.userName;
    if (userInfo.userIcon && ![userInfo.userIcon isEqualToString:@""]) {
        [_headerView.iconImageView sd_setImageWithURL:[NSURL URLWithString:userInfo.userIcon] placeholderImage:[UIImage imageNamed:@"icon_center_tx"]];
    }else {
        _headerView.iconImageView.image = [UIImage imageNamed:@"icon_center_tx"];
    }
    
//    if (!userInfo || [userInfo.userIcon isEqualToString:@""]) {
//        _headerView.iconImageView.image = [UIImage imageNamed:@"icon_center_tx"];
//    }else {
//        [_headerView.iconImageView sd_setImageWithURL:[NSURL URLWithString:userInfo.userIcon]];
//    }
    
    if (!userInfo.uid || [userInfo.uid isEqualToString:@""]) {
        footerView.hidden = YES;
    }else {
        footerView.hidden = NO;
    }
    
}


- (BOOL)goLogin {
    userInfo = [LZUserInfo getUserInfo];
    // 判断是否登录
    if (!userInfo.uid || [userInfo.uid isEqualToString:@""]) {
        JTAlertView * alert = [[JTAlertView alloc] init];
        [alert showPleaseLoginFromviewController:self loginHandel:^{
            LZLoginViewController * logVC = [[LZLoginViewController alloc] init];
            JNavigationController * navVc = [[JNavigationController alloc] initWithRootViewController:logVC];
            [self presentViewController:navVc animated:YES completion:nil];
        }];
        return NO;
    }else {
        return YES;
    }
}

// nav协议方法
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    [viewController viewWillAppear:animated];
}


@end
